var UserData = require('UserData')
var COMMON_VALUE = require('Common')
var Buildings = require('buildings')
var timeConvert = require('timeConvert')
cc.Class({
    extends: cc.Component,
    properties: {
        restaurantListNode:{
            default:null,
            type:cc.Node
        },
        restaurantPrefab:{
            default:null,
            type:cc.Prefab
        },
        multiplierButton:{
            default:null,
            type:cc.Node
        },
        recipeMenuNode:{
            default:null,
            type:cc.Node
        }
    },
    
    showUp(){        
        this.node.active = true
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.retryTimes = 0
        this.UserData =UserData
        this.Buildings = Buildings
        this.setWindowSize()
        this.getComponent()
        this.calcInc()
        if(UserData.runtimeState.sellBuildingCoolDown >0){
            this.countDownLabel.active = true
        }else{
            this.countDownLabel.active = false
        }
    },
    setWindowSize(){
        this.node.y = -this.node.height
        this.node.x = 0
        this.node.getComponent('Slipin').setOriginPos(cc.v2(0,0))
    },
    getComponent(){
        this.countDownLabel = this.node.getChildByName('卖楼').getChildByName('卖楼CD')
        this.costLabel = this.node.getChildByName('卖楼').getChildByName('框1').getChildByName('代价')
        this.gainLabel = this.node.getChildByName('卖楼').getChildByName('框1').getChildByName('收获')
        this.contractLabel = this.node.getChildByName('卖楼').getChildByName('合同文案')
    },
    calcInc(){
        let reputations_inc = Math.log(1.07,this.UserData.state.currentMoney) *10
        if(!reputations_inc){
            reputations_inc
        }
        let acumulateLevel = 0
        let levelBuff = 0 
        let coins_cost = this.UserData.state.currentMoney?this.UserData.state.currentMoney:0
        for(let i=0;i<this.UserData.state.restaurantList.length;i++){
            if(this.UserData.state.restaurantList[i]){
                acumulateLevel += this.UserData.state.restaurantList[i].Level?this.UserData.state.restaurantList[i].Level:0
            }            
        }
        //money = B x 10^N
        //Reputation = 0.25B x 10^N  
        let N = Math.floor(Math.log10(coins_cost)) 
        if(coins_cost==0){
            N=0
        }
        let B = coins_cost/Math.pow(10,N)
        levelBuff = 0.119 * B * Math.pow(10,N/3) + acumulateLevel 
        reputations_inc += levelBuff?levelBuff:0
        this.costLabel.getComponent(cc.Label).string = "店面x" + acumulateLevel 
        this.gainLabel.getComponent('labelFixed').setNumber(reputations_inc)
        this.gainLabel.getComponent('labelFixed').string = "+"+ this.gainLabel.getComponent(cc.Label).string +"奖章"
        return [levelBuff,coins_cost,reputations_inc]
    },
    sellBuilding(){
        let reputations_inc = this.calcInc()[2]
        //let coins_cost = this.calcInc()[1]
        this.Buildings.sellBuilding(0, reputations_inc).then(
            //实际操作
            ()=>{
                this.retryTimes = 0
                this.UserData.addReputation(reputations_inc)
                ///清空本地数据
                this.UserData.restaurantList = []
                this.UserData.state.currentMoney = 0
                this.UserData.state.buttonUpdateList = []
                this.UserData.state.chefStatusPrefabList = []
                this.UserData.state.moneyBuyListOrder=0
                this.UserData.state.moneyBuffList=[]
                this.UserData.state.reputationBuyListOrder=0
                this.UserData.state.reputationBuffList=[]
                this.UserData.state.customerList=[]
                this.UserData.runtimeState.sellBuildingCoolDown = 86400
                this.restaurantListNode.removeAllChildren()
                for(var i=0;i<10;i++){
                    this.restaurantListNode.insertChild(cc.instantiate(this.restaurantPrefab))
                    this.restaurantListNode.children[i].getComponent('Restaurant').floor = i+1
                }
                this.node.getComponent('Slipin').slipOut()
                this.UserData.runtimeState.GPSList = [0,0,0,0,0,0,0,0,0,0,0,0]
                this.UserData.runtimeState.totalGPS = 0
                //初始化界面数据
                this.multiplierButton.getComponent('multiplierButton').changeTimes(null,1)
                this.recipeMenuNode.getComponent('RecipeMenu').start()
            }
        ).catch((res)=>{//应该判断是返回码是不是200，或者catch到的是不是网络错误
            if(this.retryTimes>5){
                cc.director.loadScene('Loading')
            }else{
                this.sellBuilding()
                this.retryTimes+=1
            }
        })

        
    },
    refreshContractContent(){
        if(UserData.runtimeState.sellBuildingCoolDown >0){
            this.contractLabel.getComponent(cc.Label).string = "老板，还没有到交易的时间哦！一会再回来看看吧~"
        }else{
            this.contractLabel.getComponent(cc.Label).string = "大楼的租约到期了！不过没关系，老板，来卖掉你的餐厅吧。你将获得更多的奖章。你的名声远扬，会有更多的顾客光临你的下一个店面。"
        }

    },
    update(dt){
        if(UserData.runtimeState.sellBuildingCoolDown >0){
            UserData.runtimeState.sellBuildingCoolDown -= dt
            this.countDownLabel.getComponent("labelFixed").string = timeConvert(Math.floor(UserData.runtimeState.sellBuildingCoolDown))
            this.node.getChildByName('卖楼').getChildByName('开始交易按钮').getComponent(cc.Button).interactable = false
        }else{
            this.node.getChildByName('卖楼').getChildByName('开始交易按钮').getComponent(cc.Button).interactable = true
            this.countDownLabel.active = false
        }
    }
});
